// 引入 lodash 库
import cloneDeep from "lodash/cloneDeep";
/**
 * 平铺数组(深拷贝无限层平铺,适用于有children的对象数组)
 * @param {*} arr 原始数组
 * @returns 
 */
export const flatArray = (arr) => {
  if (!Array.isArray(arr) && arr.length === 0) return arr;
  const oldArr = cloneDeep(arr);
  let newArr = [];
  oldArr.forEach(item => {
    newArr.push(item);
    if (item.children && item.children.length > 0) newArr.push(...flatArray(item.children))
    delete item.children;
  });
  return newArr;
}